#!/usr/bin/env python
# encoding: utf-8

from flask_restplus import Api, Namespace, Resource, fields

api = Api()

ns = Namespace('dogs', description='Dogs related operations')

dog = ns.model('Dog', {
    'id': fields.String(required=True, description='The dog identifier'),
    'name': fields.String(required=True, description='The dog name'),
})

DOGS = [
    {'id': '1', 'name': 'Tom'},
    {'id': '2', 'name': 'Jerry'},
]


@ns.route('/')
class CatList(Resource):
    @ns.doc('list_dogs')
    @ns.marshal_list_with(dog)
    def get(self):
        # List all dogs
        return DOGS


@ns.route('/<uid>')
@ns.param('id', 'The dog identifier')
@ns.response(404, 'Cat not found')
class Cat(Resource):
    @ns.marshal_with(dog)
    def get(self, uid):
        # Fetch a dog given its identifier
        for d in DOGS:
            if d['id'] == uid:
                return d
        ns.abort(404)

    @ns.marshal_with(dog)
    @ns.doc(description='更新')
    @ns.expect(dog, validate=True)
    def put(self, uid):
        u = self.get(uid)
        n = api.payload
        u.update(n)
        return u
